package com.klangner.move.behavior
{
  import com.klangner.geom.Vector2D;
  
  public class WanderBehavior implements IBehavior
  {
    /** 
    * Distance to circel center
    */
    private var _wanderDistance:Number = 10;
    /**
    * Circle radius
    */
    private var _wanderRadius:Number = 5;
    /**
    * Last angle
    */
    private var _lastAngle:Number = 0;
    /**
    * Wander range (how much can angle change)
    */
    private var _wanderRange:Number = 1;
    
    /**
    * Constructor
    */
    public function WanderBehavior(){
    }
    
    public function updateForce(obj:ISteeredObject):void{
      
      var velocity:Vector2D = obj.velocity.clone().normalize();
      var force:Vector2D = velocity.clone().multiply(_wanderDistance);
      var offset:Vector2D = Vector2D.fromPolar(_wanderRadius, _lastAngle);
      force.add(offset);
      velocity.add(force);
      velocity.normalize();
      obj.setDesiredVelocity(velocity);
      
      _lastAngle += Math.random() * _wanderRange - _wanderRange * .5;
    }

  }
}